package com.hand.wfive.view.countdowntime;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.TextView;

/**
 * 标签制作
 *
 * @author Linhai Gu
 */
public class GradientTextView {

    private GradientDrawable mGradientDrawable;
    private TextView mLabelTextView;
    private Context mContext;
    private int labelType;
    private int padingh;
    private int padingv;

    public GradientTextView(Context _context, int type, int padingv, int padingh) {
        this.mContext = _context;
        this.labelType = type;
        this.padingv = padingv;
        this.padingh = padingh;
        mGradientDrawable = new GradientDrawable();
        mLabelTextView = new TextView(mContext);
        initGradientDrawable();
        initLabelTextView();
    }

    /**
     * 初始化GradientDrawable
     */
    private void initGradientDrawable() {
        mGradientDrawable.setColor(mContext.getResources().getColor(
                android.R.color.white));
        mGradientDrawable.setCornerRadius(dip2px(1));
        mGradientDrawable.setStroke(dip2px(1), mContext.getResources()
                .getColor(android.R.color.holo_blue_light));
        mGradientDrawable.setAlpha(255);
    }

    /**
     * 初始化标签
     */
    private void initLabelTextView() {
        mLabelTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 20);
        mLabelTextView.setGravity(Gravity.CENTER);
        if (labelType == BaseCountDownTimerView.TYPE_LABEL_TEXT) {
            mLabelTextView.setPadding(dip2px(5), dip2px(4), dip2px(5), dip2px(4));
        } else if (labelType == BaseCountDownTimerView.TYPE_LABEL_COLON1) {
            mLabelTextView.setPadding(dip2px(2), dip2px(0), dip2px(2), dip2px(0));
        } else {
            mLabelTextView.setPadding(dip2px(padingh), dip2px(padingv), dip2px(padingh), dip2px(padingv));
        }
    }

    /**
     * dp-->px
     *
     * @param dipValue
     * @return
     */
    private int dip2px(float dipValue) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    /**
     * 是否为空
     *
     * @param str
     * @return
     */
    private boolean empty(String str) {
        return TextUtils.isEmpty(str);
    }

    /**
     * 是否为空
     *
     * @param color
     * @return
     */
    private boolean empty(int color) {
        return color == -1;
    }

    /**
     * 转换成颜色值
     *
     * @param color
     * @return
     */
    private int parseColor(String color) {
        return Color.parseColor("#" + color);
    }

    /**
     * 设置边框圆角
     *
     * @param radius
     */
    public void setCornerRadius(int radius) {
        mGradientDrawable.setCornerRadius(dip2px(1));
    }

    /**
     * 标签字体颜色
     *
     * @param color
     * @return
     */
/*
    public GradientTextView setTextColor(String color) {
        if (!empty(color)) {
            try {
                mLabelTextView.setTextColor(parseColor(color));
            } catch (Exception e) {

            }
        }
        return this;
    }
*/

    /**
     * 标签字体颜色
     *
     * @param color
     * @return
     */
    public GradientTextView setTextColor(int color) {
        if (!empty(color)) {
            try {
                mLabelTextView.setTextColor(color);
            } catch (Exception e) {

            }
        }
        return this;
    }

    /**
     * 标签背景颜色
     *
     * @param color
     * @return
     */
/*    public GradientTextView setBackgroundColor(String color) {
        if (!empty(color)) {
            mGradientDrawable.setColor(parseColor(color));
        }
        return this;
    }*/

    /**
     * 标签背景颜色
     *
     * @param color
     * @return
     */
    public GradientTextView setBackgroundColor(int color) {
        if (!empty(color)) {
            mGradientDrawable.setColor(color);
        }
        return this;
    }

    /**
     * 标签外框颜色
     *
     * @param color
     * @return
     */
/*    public GradientTextView setStrokeColor(String color) {
        if (!empty(color)) {
            mGradientDrawable.setStroke(dip2px(1), parseColor(color));
        }
        return this;
    }*/

    /**
     * 标签外框颜色
     *
     * @param color
     * @return
     */
    public GradientTextView setStrokeColor(int color) {
        if (!empty(color)) {
            mGradientDrawable.setStroke(dip2px(1), color);
        }
        return this;
    }

    public GradientTextView setStrokeRadius(int radius) {
        mGradientDrawable.setCornerRadius(dip2px(radius));
        return this;
    }

    /**
     * 设置标签内容
     *
     * @param info
     * @return
     */
    public GradientTextView setLabelText(String info) {
        if (!empty(info)) {
            mLabelTextView.setText(info);
        }
        return this;
    }

    /**
     * 设置标签大小
     *
     * @param size
     * @return
     */
    public GradientTextView setTextSize(float size) {
        mLabelTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
        return this;
    }

    /**
     * 设置标签文字pading 竖向pading和横向pading
     *
     * @param StrokepadingV StrokepadingH
     * @return
     */
    public GradientTextView setTextpading(int StrokepadingV, int StrokepadingH) {
        mLabelTextView.setPadding(StrokepadingH, StrokepadingV, StrokepadingH, StrokepadingV);
        return this;
    }


    /**
     * 构造TextView
     *
     * @return
     */
    public TextView build() {
        mLabelTextView.setBackgroundDrawable(mGradientDrawable);
        return mLabelTextView;
    }

}
